package com.masterclient.app.modules.goods.newpage.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.bean.goods.CommodityActivityMessage;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.utils.TimeUtil;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.widget.AutoLineUtil;
import com.masterclient.app.widget.PriceIntegralLayout;

public class NewCommodityDetailInformationAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private boolean isShowCollect = true;

    CommodityDetailData data;

    public interface OnInformationCallback {
        void favoriteClick();

        void upgradeClick();
    }

    OnInformationCallback onInformationCallback;

    public OnInformationCallback getOnInformationCallback() {
        return onInformationCallback;
    }

    public void setOnInformationCallback(OnInformationCallback onInformationCallback) {
        this.onInformationCallback = onInformationCallback;
    }

    public NewCommodityDetailInformationAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null, true);
    }

    public NewCommodityDetailInformationAdapter(Activity context, LayoutHelper layoutHelper, int count,
                                                @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data, boolean isShowCollect) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        this.isShowCollect = isShowCollect;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    private int spec;
    private int popupWidth;
    private int popupHeight;
    private String mCopiedText;

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        ViewGroup ll_price_container = holder.itemView.findViewById(R.id.ll_price_container);
        View view_limit = holder.itemView.findViewById(R.id.view_limit);
        TextView sale_progress = holder.itemView.findViewById(R.id.sale_progress);
        ProgressBar progressBar = holder.itemView.findViewById(R.id.progressBar);
        LinearLayout detailPriceLayout = holder.itemView.findViewById(R.id.detail_price_layout);
        TextView tvDayTxt = holder.itemView.findViewById(R.id.tv_day_txt);
        TextView tvDayPrice = holder.itemView.findViewById(R.id.tv_day_price);
        TextView tvOfficialTxt = holder.itemView.findViewById(R.id.tv_official_txt);
        TextView tvOfficialPrice = holder.itemView.findViewById(R.id.tv_official_price);
        TextView tvShareEarn = holder.itemView.findViewById(R.id.tv_share_earn);
        AutoLineUtil llUser = holder.itemView.findViewById(R.id.ll_user);
        detailPriceLayout.setVisibility("2".equals(data.getProduct_type()) ? View.GONE : View.VISIBLE);

        //日常价
        if (data.getPriceTagBean() != null && !TextUtils.isEmpty(data.getPriceTagBean().getBase_price_tag())) {
            tvDayTxt.setText(data.getPriceTagBean().getBase_price_tag() + ":");
            if (!TextUtils.isEmpty(data.getDaily_price())) {
                tvDayPrice.setText("¥" + data.getDaily_price());
                tvDayPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                tvDayPrice.setVisibility(View.VISIBLE);
            }
            tvDayTxt.setVisibility(View.VISIBLE);
        } else {
            tvDayTxt.setVisibility(View.GONE);
            tvDayPrice.setVisibility(View.GONE);
        }

        //官方价
        if (!"2".equals(data.getProduct_type()) && data.getPriceTagBean() != null && !TextUtils.isEmpty(data.getPriceTagBean().getMarket_price_tag())) {
            tvOfficialTxt.setText(data.getPriceTagBean().getMarket_price_tag() + ":");
            if (!TextUtils.isEmpty(data.getMarket_price())) {
                tvOfficialPrice.setText("¥" + data.getMarket_price());
                tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                tvOfficialPrice.setVisibility(View.VISIBLE);
            }
            tvOfficialTxt.setVisibility(View.VISIBLE);
        } else {
            tvOfficialTxt.setVisibility(View.GONE);
            tvOfficialPrice.setVisibility(View.GONE);
        }

        //分享赚
        if (TextUtils.isEmpty(data.getCommission()) || 0 == Double.valueOf(data.getCommission())) {
            tvShareEarn.setVisibility(View.GONE);
        } else {
            tvShareEarn.setText("分享赚佣金￥" + data.getCommission()+"元");
            tvShareEarn.setVisibility(View.VISIBLE);
        }

        //tags
        if (data.getProduct_tags() != null && data.getProduct_tags().size()>0){
            llUser.setVisibility(View.VISIBLE);
            for (int i=0;i < data.getProduct_tags().size(); i++){
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_new_goods_details_tag,null,false);
                TextView tvTag =view.findViewById(R.id.tv_tag);
                tvTag.setText(data.getProduct_tags().get(i));
                llUser.addView(view);
            }
        }else {
            llUser.setVisibility(View.GONE);
        }


        //价格行
        PriceIntegralLayout ll_price = holder.itemView.findViewById(R.id.ll_price);
        ll_price.setData(data.getIntegral_price(), data.getPrice());
        ll_price.floadSize(1);

        //标题
        TextView sub_title_txt = holder.itemView.findViewById(R.id.sub_title_txt);
        if (TextUtils.isEmpty(data.getTitle())) {
            sub_title_txt.setVisibility(View.GONE);
        } else {
            sub_title_txt.setVisibility(View.VISIBLE);
            sub_title_txt.setText(data.getTitle());
        }

        // 价格背景设置
        CommodityActivityMessage commodityActivityMessage = data.getSingle_promotion();
        boolean isShowBlackBg = false;
        if (commodityActivityMessage != null && (!TimeUtil.isBeforeNow(commodityActivityMessage.getStart_at()) || !TimeUtil.isBeforeNow(commodityActivityMessage.getEnd_at()))) {
            isShowBlackBg = true;
        }

        if (isShowBlackBg) {
            ll_price_container.setBackgroundResource(R.mipmap.bg_goods_vip_price);
            ll_price.setPriceTextColor(Color.parseColor("#EED29C"));

            if ("0".equals(data.getSingle_promotion().getDisplay())){
                view_limit.setVisibility(View.GONE);
            }else{
                view_limit.setVisibility(View.VISIBLE);
                sale_progress.setText("已售"+data.getSingle_promotion().getDisplay() + "%");
                int progress = (int) Float.valueOf(data.getSingle_promotion().getDisplay()).floatValue();
                progressBar.setProgress(progress);
            }
        } else {
            ll_price_container.setBackgroundColor(Color.WHITE);
            ll_price.setPriceTextColor(Color.parseColor("#333333"));

            view_limit.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 2;
    }
}
